<?php
/**
 * This class is a class adapter between DrupalUser and RM_User_Row
 */
class RM_Drupal_User  implements RM_User_CMS_Interface {
      /**
     * @var DrupalUser
     */
    private $_user;

    /**
     * Constructor
     * @param DrupalUser $user
     */
    public function  __construct($user) {
        $this->_user = $user;
    }

    /**
     * This method indicates is CMS user a guest or already registered CMS user
     * @return bool
     */
    public function isGuest(){
      //in drupal id guest = 0
      return $this->_user->uid == 0;
    }

    /**
     * Tries to find a Resmania user that linked to CMS user, if not returns null
     * @return RM_User_Row|null
     */
    public function findResmaniaUser(){
        $model = new RM_Users();
        $resmaniaUser = $model->getByField('cms_id', $this->_user->uid)->current();
        return $resmaniaUser;
    }

    /**
     * Convert CMS user objcet into RM_User_Row object (without saving it into database)
     * @return RM_User_Row
     */
    public function convertToResmaniaUser(){
        $model = new RM_Users();
        $user = $model->createRow();
        $user->cms_id = $this->_user->uid;
        $user->first_name = $this->_user->name;
        $user->username = $this->_user->name;
        $user->email = $this->_user->mail;
        
        return $user;
    }

    /**
     * Return internal object User this method should be called only from Drupal connector classes
     *
     * @return drupal User
     */
    public function getUser(){
        return $this->_user;
    }
}